﻿using community.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace community.Controllers
{
    [Route("api/houses")]
    [ApiController]
    public class HousesController : ControllerBase
    {
        private static List<House> houses = new List<House>();

        // 房屋登记
        [HttpPost]
        public IActionResult RegisterHouse([FromBody] House house)
        {
            house.Id = houses.Count + 1;
            house.CreatedAt = DateTime.UtcNow;
            house.UpdatedAt = DateTime.UtcNow;
            houses.Add(house);
            return CreatedAtAction(nameof(GetHouseById), new { id = house.Id }, house);
        }

        // 查询所有房屋
        [HttpGet]
        public IActionResult GetAllHouses()
        {
            return Ok(houses);
        }

        // 根据ID查询房屋信息
        [HttpGet("{id}")]
        public IActionResult GetHouseById(int id)
        {
            var house = houses.Find(h => h.Id == id);
            if (house == null)
            {
                return NotFound();
            }
            return Ok(house);
        }

        // 更新房屋信息
        [HttpPut("{id}")]
        public IActionResult UpdateHouse(int id, [FromBody] House updatedHouse)
        {
            var house = houses.Find(h => h.Id == id);
            if (house == null)
            {
                return NotFound();
            }

            house.BuildingNo = updatedHouse.BuildingNo;
            house.UnitNo = updatedHouse.UnitNo;
            house.RoomNo = updatedHouse.RoomNo;
            house.OwnerId = updatedHouse.OwnerId;
            house.Status = updatedHouse.Status;
            house.Area = updatedHouse.Area;
            house.UpdatedAt = DateTime.UtcNow;

            return NoContent();
        }

        // 删除房屋信息
        [HttpDelete("{id}")]
        public IActionResult DeleteHouse(int id)
        {
            var house = houses.Find(h => h.Id == id);
            if (house == null)
            {
                return NotFound();
            }

            houses.Remove(house);
            return NoContent();
        }
    }
}
